/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.unittests;

import org.aroundme.model.ApplicationSecurityManager;
import org.aroundme.model.Encrypter;
import org.aroundme.model.User;
import org.aroundme.unitTools.TestFixture;
import org.bouncycastle.util.encoders.Hex;

public class ApplicationSecurityManagerTest extends TestFixture {
	private ApplicationSecurityManager securityMan = null;
	private Encrypter encr;
	
	protected void setUp() throws Exception {
		super.setUp();
		securityMan = (ApplicationSecurityManager) springFactory.getBean(ApplicationSecurityManager.class.getSimpleName());
		encr = new Encrypter();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		securityMan = null;
		encr = null;
	}

	public void testCheckLogin() {
		xmlTestUtils.saveDummyUsersToDB();
		
		User user = xmlTestUtils.getDummyUsers().get(0);
		//create an encrypted message object (would take place on client-side)
		byte[] encrPass = encr.encryptPassword(user.getPassword());
		
		String pass = new String(Hex.encode(encrPass));
		
		boolean isValid = securityMan.checkLogin(user.getLoginName(), pass);
		
		assertTrue("login should be valid", isValid);
	}

}
